import requests
import math

def get_login():
    url = 'https://gateway.bilinl.com/uaa/login/acct'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
    }
    data = {
        'clientId': '83YslrcMpBt3X3c0eK8jvhdPAW84ScHc',
        'clientSecret': 'rosJtPuAMy69wnEMiw9UqRaQNELypEyk',
        'username': 'datacenter',
        'password': '.datacenter.jf',
        'phoneLogin': '0'
    }
    res = requests.post(url,headers=headers,data=data)
    result = res.json()
    value = result['data']['value']
    return value


# 处理中的数据
def get_data(value):
    url = 'https://gateway.bilinl.com/kfweb/Chat/GetUserListByType'
    headers = {
        'authorization': 'Bearer %s'%value,
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
        'content-type': 'application/json'
    }
    data ='{"nType":10,"nPageIndex":1,"nPageSize":10,"nState":10,"dtStartTime":"2021-07-29","dtEndTime":"2021-07-29","vcSearchKey":"","vcSearchType":0,"nOverParentId":0,"nOverChildId":0}'
    res = requests.post(url,headers=headers,data=data)
    result = res.json()
    for data in result['data']['UserList']:
        get_detail_data(value,data['vcCustomerID'],data['vcRobotWxID'])

def get_detail_data(value,vcCustomerID,vcRobotWxID):
    url = 'https://gateway.bilinl.com/kfweb/Chat/GetMsgDetail'
    headers = {
        'authorization': 'Bearer %s'%value,
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
        'content-type': 'application/json'
    }
    data = '{"nType":10,"vcUser":"datacenter","vcCustomerID":"%s","vcRobotWxID":"%s","dtStartTime":"2021-07-29","vcSearchKey":"","nPageIndex":1,"nPageSize":10,"nMessageTypes":[],"nId":0}'%(vcCustomerID,vcRobotWxID)
    res = requests.post(url,headers=headers,data=data)
    result = res.json()
    for data in result['data']['Table1']:
        dtCreateTime = data['dtCreateTime']
        dtMsgTime = data['dtMsgTime']
        vcCustomerName = data['vcCustomerName']
        vcCustomerID = data['vcCustomerID']
        vcCustomerWxID = data['vcCustomerWxID']
        vcContent = data['vcContent']
        nIsGetData = math.ceil(int(data['nIsGetData'])-10)
        vcMsgId = data['vcMsgId']
        vcRobotWxID = data['vcRobotWxID']
        vcSendMsgId = data['vcSendMsgId']
        print('第一页======'+dtCreateTime,'\n'+dtMsgTime,'\n'+vcCustomerName,'\n'+vcCustomerID,'\n'+vcCustomerWxID,'\n'+vcContent,'\n'+str(nIsGetData),'\n'+vcMsgId,'\n'+vcRobotWxID,'\n'+vcSendMsgId)
    print('=============',result['data']['Table1'][0])
    # if str(result['data']['Table1'][0])>10:
    #     for i in range(2,math.ceil(result['data']['Table1'][0]/10)+1):
    #         data = '{"nType":10,"vcUser":"datacenter","vcCustomerID":"64ED44566378C2308025CABF7AD77E60","vcRobotWxID":"0CAE484C49C3A644B29E0FC36AB67F3C","dtStartTime":"2021-07-29","vcSearchKey":"","nPageIndex":%s,"nPageSize":10,"nMessageTypes":[],"nId":0}'%str(i)
    #         res = requests.post(url,headers=headers,data=data)
    #         result = res.json()
    #         for data in result['data']['Table1']:
    #             dtCreateTime = data['dtCreateTime']
    #             dtMsgTime = data['dtMsgTime']
    #             vcCustomerName = data['vcCustomerName']
    #             vcCustomerID = data['vcCustomerID']
    #             vcCustomerWxID = data['vcCustomerWxID']
    #             vcContent = data['vcContent']
    #             nIsGetData = math.ceil(int(data['nIsGetData'])-10)
    #             vcMsgId = data['vcMsgId']
    #             vcRobotWxID = data['vcRobotWxID']
    #             vcSendMsgId = data['vcSendMsgId']
    #             print('第'+str(i)+'页======'+dtCreateTime,'\n'+dtMsgTime,'\n'+vcCustomerName,'\n'+vcCustomerID,'\n'+vcCustomerWxID,'\n'+vcContent,'\n'+str(nIsGetData),'\n'+vcMsgId,'\n'+vcRobotWxID,'\n'+vcSendMsgId)




if __name__ == "__main__":
    value = get_login()
    get_data(value)
    # get_detail_data()